package com.xiao.wiki.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author ：降蓝
 * @description：TODO
 * @date ：2022/9/14 23:35
 */
@Component
public class RedisUtil {
    private static final Logger LOG = LoggerFactory.getLogger(RedisUtil.class);

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    public boolean validateRepeat(String key, long second) {
        if (stringRedisTemplate.hasKey(key)) {
            LOG.info("key已存在：{}", key);
            return false;
        } else {
            LOG.info("key不存在，放入：{}，过期 {} 秒", key, second);
            stringRedisTemplate.opsForValue().set(key, key, second, TimeUnit.SECONDS);
            return true;
        }

    }

}
